<template>
  <div class="container">
    <el-form 
      :model="formData" 
      :rules="rules" 
      ref="form" 
      label-width="100px" 
      class="password-form"
    >
      <div class="form-header">
        <h2 class="form-title">修改登录密码</h2>
        <p class="form-desc">请按要求输入新旧密码</p>
      </div>

      <el-form-item label="旧密码" prop="oldPassword" class="form-item">
        <el-input 
          v-model="formData.oldPassword" 
          type="password" 
          placeholder="请输入当前使用的密码"
          class="custom-input"
        ></el-input>
      </el-form-item>

      <el-form-item label="新密码" prop="newPassword" class="form-item">
        <el-input 
          v-model="formData.newPassword" 
          show-password
          placeholder="请输入6-16位新密码"
          class="custom-input"
          :rules="rules.newPassword"
        ></el-input>
      </el-form-item>

      <el-form-item label="确认密码" prop="confirmPassword" class="form-item">
        <el-input 
          v-model="formData.confirmPassword" 
          show-password
          placeholder="请再次输入新密码"
          class="custom-input"
          :rules="rules.confirmPassword"
        ></el-input>
      </el-form-item>

      <el-form-item class="button-group">
        <el-button 
          type="primary" 
          @click="changePassword" 
          class="submit-btn"
        >立即修改</el-button>
      </el-form-item>

      <el-form-item v-if="error" class="error-message">
        <i class="el-icon-error"></i>
        <span>{{ error }}</span>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, onBeforeMount } from "vue";
import { useStore } from "vuex";
// 模拟 toast 提示，实际若有需求可替换成项目里的提示组件
const toast = (msg, type = 'success') => {
  console.log(`${type === 'success' ? '✅' : '❌'} ${msg}`);
};

const store = useStore();
const form = ref(null);
const formData = ref({
  oldPassword: "",
  newPassword: "",
  confirmPassword: "",
});

const rules = ref({
  oldPassword: [
    { required: true, message: "请输入旧密码", trigger: "blur" }
  ],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { pattern: /^.{6,16}$/, message: "新密码需为6-16位", trigger: "blur" }
  ],
  confirmPassword: [
    { required: true, message: "请再次输入新密码", trigger: "blur" },
    { 
      validator: (rule, value, callback) => {
        if (value !== formData.value.newPassword) {
          callback(new Error("两次输入的密码不同"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
});

const error = ref("");

const changePassword = () => {
  form.value.validate(async (valid) => {
    if (valid) {
      const user = JSON.parse(localStorage.getItem('user') || '{}');
      if (formData.value.oldPassword !== user.password) {
        toast("旧密码错误，请重试", "error");
        return;
      }

      await store.dispatch("updatePassword", formData.value.newPassword);
      toast("密码修改成功", "success");
      form.value.resetFields();
    }
  });
};

onBeforeMount(async () => {
  await store.dispatch("getinfo");
});
</script>

<style scoped>
.container {
  width: 400px;
  margin: 50px auto;
  padding: 30px;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.form-header {
  text-align: center;
  margin-bottom: 20px;
}

.form-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.form-desc {
  font-size: 14px;
  color: #999;
}

.form-item {
  margin-bottom: 20px;
}

.custom-input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid #dcdcdc;
  border-radius: 6px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s ease;
}

.custom-input:focus {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
}

.button-group {
  text-align: center;
}

.submit-btn {
  padding: 10px 30px;
  font-size: 14px;
  border-radius: 6px;
  background-color: #409eff;
  color: #fff;
  border: none;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.submit-btn:hover {
  background-color: #66b1ff;
}

.error-message {
  color: #f56c6c;
  font-size: 12px;
  margin-top: 10px;
}
</style>